package com.hengtuo.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtuo.kaiyuanzhongguo.R;
import com.hengtuo.util.AcquisitionTime;
import com.hengtuo.util.ImageLoderUtils;
import com.hengtuo.vo.Tweet;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by msi on 2016/5/9.
 */
public class MoveballAdapter extends BaseAdapter {
    Context context;
    List<Tweet> list;
    DisplayImageOptions options = ImageLoderUtils.getDisplayImageOptions(context);
    private String name;
    List<String> n = new ArrayList<>();

    public MoveballAdapter(Context context, List<Tweet> list) {
        this.context = context;
        this.list = list;
        ImageLoderUtils.getImageLoaderConfiger(context);
        ImageLoderUtils.getImageLoderInit(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder h = null;
//          if (convertView == null) {
            h = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.moveball_item, null);

            h.iv_moveball_portrait = (ImageView) convertView.findViewById(R.id.iv_moveball_portrait);
            h.tv_moveball_author = (TextView) convertView.findViewById(R.id.tv_moveball_author);
            h.tv_moveball_body = (TextView) convertView.findViewById(R.id.tv_moveball_body);
            h.tv_moveball_time = (TextView) convertView.findViewById(R.id.tv_moveball_time);
            h.tv_moveball_commentCount = (TextView) convertView.findViewById(R.id.tv_moveball_commentCount);
            h.rl_moveball_praise = (LinearLayout) convertView.findViewById(R.id.rl_moveball_praise);


//            convertView.setTag(h);
//        } else {
//            h = (Holder) convertView.getTag();
//        }

        ImageLoader.getInstance().displayImage(list.get(position).getPortrait(), h.iv_moveball_portrait, options);

        String user_time = AcquisitionTime.dateToTimestamp(list.get(position).getPubDate());

        String time = AcquisitionTime.getStandardDate(user_time);

        h.tv_moveball_author.setText(list.get(position).getAuthor());
        h.tv_moveball_body.setText(list.get(position).getBody());
        h.tv_moveball_time.setText(time);
        h.tv_moveball_commentCount.setText(list.get(position).getCommentCount());


        if (list.get(position).getLikeList().getUser() != null) {

//            for (int i = 0; i < list.get(position).getLikeList().getUser().size(); i++) {
//
//                name = list.get(position).getLikeList().getUser().get(i).getName();
//
//                n.add(name);
//
//            }
//            ArrayAdapter adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, n);
            h.rl_moveball_praise.setVisibility(View.VISIBLE);
//            MoveballPraiseAdapter adapter=new MoveballPraiseAdapter( list.get(position).getLikeList().getUser(),context);
//            // 获取listview实例
//            h.gv_moveball_praise.setAdapter(adapter);
//
//            h.gv_moveball_praise.setStretchMode(GridView.NO_STRETCH);   //不拉伸
//            h.tv_moveball_praise.setText(name);

//            View view = LayoutInflater.from(context).inflate(R.layout.moveball_grid_item, null);
//            TextView tv_moveball_grid_praise = (TextView) view.findViewById(R.id.tv_moveball_grid_praise);
//
//           h.gv_moveball_praise.setColumnWidth(MoveballPraiseAdapter.i);   //列宽（像素）


//            h.tv1_moveball_praise.setText(list.get(position).getLikeList().getUser().get(0).getName());
            TextView t = null;
            for (int i = 0; i < 4; i++) {

                if (i < list.get(position).getLikeList().getUser().size()) {

                    t = new TextView(context);
                    h.rl_moveball_praise.addView(t);
                    t.setText(list.get(position).getLikeList().getUser().get(i).getName() + "、");
                    t.setTextSize(10);
                    Log.d("MoveballAdapter", list.get(position).getLikeList().getUser().get(i).getName());

                } else {

                    h.rl_moveball_praise.removeView(t);

                }
            }


        } else {

            h.rl_moveball_praise.setVisibility(View.GONE);

        }

        // h. tv_moveball_praise.setText(list.get(position).getLikeList().getUser().get(position).getName());


        return convertView;
    }

    class Holder {
        ImageView iv_moveball_portrait;
        TextView tv_moveball_author;
        TextView tv_moveball_body;
        TextView tv_moveball_time;
        TextView tv_moveball_commentCount;
        LinearLayout rl_moveball_praise;


    }

}
